/*
 * ClassWindow.cpp
 *
 *  Created on: 12/06/2011
 *      Author: dela
 */

#include "ClassWindow.h"

#include <string>
#include <sstream>
#include <iostream>
#include <list>
#include "common/Course.h"
#include "../Connection.h"

using namespace std;

/**
 *
 */
ClassWindow::ClassWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) :
	Gtk::Window(cobject), builder(builder) {
	c.setId(0);

	// Boton guardar y salir:
	Gtk::Button *button = 0;
	builder->get_widget("button1", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &ClassWindow::onSave));

	// Boton Cancelar:
	builder->get_widget("button2", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &ClassWindow::hide));

	// OnShow:
	this->signal_show().connect(
			sigc::mem_fun(*this, &ClassWindow::onShow));

	// Combo:
	Gtk::Table *table;
	builder->get_widget("table2", table);
	table->attach(combo, 4, 5, 2, 3);
	if (Connection::getInstance()->getType() == 'S')
		combo.set_sensitive(false);
	combo.show();
}


/**
 *
 */
void ClassWindow::onShow() {
	Gtk::Entry *input;
	builder->get_widget("entry1", input);
	std::ostringstream sout;
	sout << c.getId();
	input->set_text(sout.str());
	builder->get_widget("entry3", input);
	input->set_text(c.getName());
	builder->get_widget("entry2", input);
	input->set_text(c.getStartDate());
	builder->get_widget("entry4", input);
	input->set_text(c.getDuration());
	builder->get_widget("entry5", input);
	input->set_text(c.getPassword());
	Gtk::TextView *textView;
	builder->get_widget("textview1", textView);
	textView->get_buffer()->set_text(c.getDescription());

	//Combo de cursos:
	combo.append_text("");
	list<Course> courses;
	list<Course>::iterator it;
	Connection::getInstance()->getCourses(courses);
	for(it = courses.begin(); it != courses.end(); it++) {
		std::ostringstream sout;
		sout << it->getId();
		combo.append_text(sout.str());
	}
	if (c.getCourseId() == 0) {
		combo.set_active_text("");
	} else {
		std::ostringstream sout;
		sout << c.getCourseId();
		combo.set_active_text(sout.str());
	}
}


/**
 *
 */
void ClassWindow::onSave() {
	string id, name, startDate, duration, password, description;
	Gtk::Entry *input;
	builder->get_widget("entry1", input);
	id = input->get_text();
	builder->get_widget("entry3", input);
	name = input->get_text();
	builder->get_widget("entry2", input);
	startDate = input->get_text();
	builder->get_widget("entry4", input);
	duration = input->get_text();
	builder->get_widget("entry5", input);
	password = input->get_text();
	Gtk::TextView *textView;
	builder->get_widget("textview1", textView);
	description = textView->get_buffer()->get_text();

	c.setId(atol(id.c_str()));
	c.setName(name);
	c.setStartDate(startDate);
	c.setDuration(duration);
	if (password.size() > 0)
		c.setHasPassword(true);
	else
		c.setHasPassword(false);
	c.setPassword(password);
	c.setDescription(description);
	string aux(combo.get_active_text());
	if ( aux == "" )
		c.setCourseId( 0 );
	else
		c.setCourseId( atol(aux.c_str()) );

	Connection::getInstance()->createClass(c);

	this->hide();
}
